package development.bussiness;

import development.Global;
import development.bean.ChatMessage;
import development.bean.ChatRoom;
import development.socket.ProtocolConstants;
import development.socket.SocketClientUtil;
import state.Alert;
import state.AlertListener;
import state.GameGlobal;
import state.socket.DataPackage;

public class ChatBussiness implements AlertListener {
  public void onCreateRoomSuccess(DataPackage dataPackage) {
    // Get data
    String roomName = dataPackage.nextString();
    String title = dataPackage.nextString();
    String roomMaster = dataPackage.nextString();
    boolean hasPassword = dataPackage.nextInt() == 0 ? false : true;
    int conCurrentUser = dataPackage.nextInt();
    int maxUser = dataPackage.nextInt();
    
    // Set value
    Global.currentChatRoom = new ChatRoom();
    Global.currentChatRoom.setName(roomName);
    Global.currentChatRoom.setTitle(title);
    Global.currentChatRoom.setRoomMaster(roomMaster);
    Global.currentChatRoom.setHasPassword(hasPassword);
    Global.currentChatRoom.setConcurrentUser(conCurrentUser);
    Global.currentChatRoom.setMaxUser(maxUser);
    
    Global.chatRoomMap.put(roomName, Global.currentChatRoom);
    GameGlobal.nextState(Global.frmChatRoom, null);
  }
  
  public void onCreateRoomFail(DataPackage dataPackage) {
    int errorCode = dataPackage.nextInt();
    switch (errorCode) {
    case ProtocolConstants.ErrorCode.DUPPLICATE_ROOM_ERROR:
      GameGlobal.alert.showAlert(this, Alert.OK_TYPE, new String[] {"Tạo phòng thất bại", "Số hiệu phòng đã tồn tại"});
      break;
    case ProtocolConstants.ErrorCode.MAX_USER_MUST_GREATER_THAN_ZERO:
      GameGlobal.alert.showAlert(this, Alert.OK_TYPE, new String[] {"Tạo phòng thất bại", "Số thành viên phải lớn hơn 0"});
      break;
    }
  }
  
  public void onJoinChatRoomSuccess(DataPackage dataPackage) {
    String roomName = dataPackage.nextString();
    String title = dataPackage.nextString();
    String roomMaster = dataPackage.nextString();
    boolean hasPassword = dataPackage.nextInt() == 0 ? false : true;
    int conCurrentUser = dataPackage.nextInt();
    int maxUser = dataPackage.nextInt();
    
    // Check if room is exits in client or not
    ChatRoom chatRoom = (ChatRoom) Global.chatRoomMap.get(roomName);
    if (chatRoom == null) {
      Global.currentChatRoom = new ChatRoom();
      Global.chatRoomMap.put(roomName, Global.currentChatRoom);
    } else {
      Global.currentChatRoom = chatRoom;
    }
    
    // Set value
    Global.currentChatRoom.setName(roomName);
    Global.currentChatRoom.setTitle(title);
    Global.currentChatRoom.setRoomMaster(roomMaster);
    Global.currentChatRoom.setHasPassword(hasPassword);
    Global.currentChatRoom.setConcurrentUser(conCurrentUser);
    Global.currentChatRoom.setMaxUser(maxUser);
    GameGlobal.nextState(Global.frmChatRoom, null);
  }
  
  public void onJoinChatRoomFail(DataPackage dataPackage) {
    SocketClientUtil.requestChatRoomList();
    int errorCode = dataPackage.nextInt();
    switch (errorCode) {
    case ProtocolConstants.ErrorCode.ROOM_DOES_NOT_EXIST_ERROR:
      GameGlobal.alert.showAlert(this, Alert.OK_TYPE, "Phòng chat không tồn tại");
      break;
    case ProtocolConstants.ErrorCode.CHAT_ROOM_FULL_ERROR:
      GameGlobal.alert.showAlert(this, Alert.OK_TYPE, "Phòng chat đã đầy");
      break;
    case ProtocolConstants.ErrorCode.CHAT_ROOM_PASSWORD_WRONG:
      GameGlobal.alert.showAlert(this, Alert.OK_TYPE, new String[] {"Tạo phòng thất bại", "Mật khẩu sai"});
      break;
    }
  }
  
  public void onMessageReceive(DataPackage dataPackage) {
    String fromUser = dataPackage.nextString();
    String target = dataPackage.nextString();
    String message = dataPackage.nextString();
    
    // If there is private message
    if (target.equals(Global.currentUser.getName())) {
      ChatMessage chatMessage = (ChatMessage) Global.privateMessageMap.get(fromUser);
      if (chatMessage == null) {
        chatMessage = new ChatMessage();
        chatMessage.setFromUser(fromUser);
        Global.privateMessageMap.put(fromUser, chatMessage);
      }
      chatMessage.appendMessage(message); 
      notifyNewMessageToUI(fromUser, message);
      return;
    }
    
    // If there is room chat message
    ChatRoom chatRoom = (ChatRoom) Global.chatRoomMap.get(target);
    if (chatRoom == null) {
      chatRoom = new ChatRoom().setName(target);
      Global.chatRoomMap.put(target, chatRoom);
    }
    
    // Put chat message to room
    ChatMessage chatMessage = new ChatMessage();
    chatMessage.setFromUser(fromUser);
    chatMessage.appendMessage(message);
    chatRoom.putMessage(chatMessage);
    notifyNewMessageToUI(fromUser, message);
  }
  
  private void notifyNewMessageToUI(String fromUser, String message) {
    // TODO: notifyNewMessageToUI
  }
  
  public void onUserGetInNotify(DataPackage dataPackage) {
    String userName = dataPackage.nextString();
    String roomName = dataPackage.nextString();
    
    ChatRoom room = (ChatRoom) Global.chatRoomMap.get(roomName);
    if (room != null) {
      room.putMember(userName);
      notiFyNewMemberGetInRoom(userName, roomName);
    }
  }
  
  private void notiFyNewMemberGetInRoom(String userName, String roomName) {
    // TODO: notiFyNewMemberGetInRoom
  }
  
  public void onUserGetOutNotify(DataPackage dataPackage) {
    String userName = dataPackage.nextString();
    String roomName = dataPackage.nextString();
    
    ChatRoom room = (ChatRoom) Global.chatRoomMap.get(roomName);
    if (room != null) {
      room.removeMember(userName);
      notiFyNewMemberGetOutRoom(userName, roomName);
    }
  }
  
  private void notiFyNewMemberGetOutRoom(String userName, String roomName) {
    // TODO: notiFyNewMemberGetOutRoom
  }
  
  public void onReceiveChatRoomList(DataPackage dataPackage) {
    Global.chatRoomMap.clear();
    int numberOfRoom = dataPackage.nextInt();
    for (int i = 0; i < numberOfRoom; i++) {
      ChatRoom room = new ChatRoom();
      room.setName(dataPackage.nextString());
      room.setTitle(dataPackage.nextString());
      room.setRoomMaster(dataPackage.nextString());
      room.setHasPassword(dataPackage.nextInt() == 0 ? false : true);
      room.setConcurrentUser(dataPackage.nextInt());
      room.setMaxUser(dataPackage.nextInt());
      Global.chatRoomMap.put(room.getName(), room);
    }
  }
  
  public void onLeaveRoomSuccess(DataPackage dataPackage) {
  }
  
  public void onLeaveRoomFail(DataPackage dataPackage) {
  }
  
  public void onRoomMemberListReceive(DataPackage dataPackage) {
    // Get value from dataPackage
    String roomName = dataPackage.nextString();
    int size = dataPackage.nextInt();
    
    // Check if room is exits in client or not
    ChatRoom chatRoom = (ChatRoom) Global.chatRoomMap.get(roomName);
    if (chatRoom == null) {
      Global.currentChatRoom = new ChatRoom();
      Global.chatRoomMap.put(roomName, Global.currentChatRoom);
      chatRoom = Global.currentChatRoom;
    } else {
      Global.currentChatRoom = chatRoom;
    }
    
    chatRoom.removeAllMembers();
    for (int i = 0; i < size; i++) {
      chatRoom.putMember(dataPackage.nextString());
    }
  }
  
  public void onGetRoomMemberListFail(DataPackage dataPackage) {
    int errorCode = dataPackage.nextInt();
    String chatRoomName = dataPackage.nextString();
    
    String[] errorMessages = {""};
    switch (errorCode) {
    case ProtocolConstants.ErrorCode.ROOM_DOES_NOT_EXIST_ERROR:
      errorMessages[0] = "Phòng chat " + chatRoomName + " không tồn tại.";
      SocketClientUtil.requestChatRoomList();
      break;
    case ProtocolConstants.ErrorCode.CHAT_MUST_BE_ROOM_MEMBER_TO_DO_ACTION:
      errorMessages[0] = "Vào phòng " + chatRoomName + " thất bại.";
      break;
    }
    GameGlobal.alert.showAlert(this, Alert.OK_TYPE, errorMessages);
  }
  
  public void onChangeRoomPasswordSuccess(DataPackage dataPackage) {
  }
  
  public void onChangeRoomPasswordFail(DataPackage dataPackage) {
  }
  
  public void onKickMemberSuccess(DataPackage dataPackage) {
  }
  
  public void onKickMemberFail(DataPackage dataPackage) {
  }
  
  public void onBlockUserSuccess(DataPackage dataPackage) {
  }
  
  public void onBlockUserFail(DataPackage dataPackage) {
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
  }
}
